package com.candy.control;

import com.candy.model.LoginModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        处理请求相应的字符集，避免中文乱码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
//        获取页面传递进来的参数
      String username=req.getParameter("username");
      String userPsw=req.getParameter("userpwd");
      System.out.println("username:"+username+";userpwd:"+userPsw);
//      调用业务逻辑处理层
      LoginModel lm=new LoginModel();
//      判断用户名密码比对结果
      boolean ok= lm.logined(username,userPsw);
//      如果返回值为true，表示用户名密码匹配成功，则跳转到登录成功页面
     if(ok==true){
//         转发
         req.getRequestDispatcher("logined.jsp").forward(req,resp);
     }else {
//         反之，返回登录页面
//         重定向
         resp.sendRedirect("index.jsp");
     }





    }
}
